import logging

import requests
import unittest

from api.loginAPI import loginAPI
from api.tenderAPI import tenderAPI
from utils import assert_utils, request_third_api


class tender(unittest.TestCase):
    tender_id = 2167
    def setUp(self) -> None:
        self.login_api = loginAPI()
        self.tender_api = tenderAPI()
        self.session = requests.Session()

        response = self.login_api.login(self.session,keywords="17688880002")
        logging.info("login response = {}".format(response.json()))
        assert_utils(self,response,200,200,'登录成功')

    def tearDown(self) -> None:
        self.session.close()

    # 获取投资产品详情
    def test01_get_loaninfo(self):
        response = self.tender_api.get_loaninfo(self.session,self.tender_id)
        logging.info('get loaninfo response = {}'.format(response.json()))
        assert_utils(self,response,200,200,"OK")
        self.assertEqual('2167',response.json().get('data').get('loan_info').get('id'))

    # 投资
    def test02_tender(self):
        response = self.tender_api.tender(self.session,self.tender_id,amount='100')
        logging.info('tender response = {}'.format(response.json()))
        self.assertEqual(200,response.status_code)
        self.assertEqual(200,response.json().get("status"))

        form_data = response.json().get("description").get('form')
        logging.info('form response = {}'.format(form_data))

        response = request_third_api(form_data)
        logging.info("third-interface response={}".format(response.text))
        self.assertEqual('InitiativeTender OK', response.text)

    # 获取投资列表
    def test03_get_tenderList(self):
        response = self.tender_api.get_tenderlist(self.session,'tender')
        logging.info('tenderList response = {}'.format(response.json()))
        self.assertEqual(200,response.status_code)




